package J20241104_high_precision;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description: 高精度加法
 * User: 王圆豪
 * Date: 2024-11-04
 * Time: 21:35
 */

public class High_precision_addition {
    public static void main(String[] args) {
        String s1 = "115196516265206";
        String s2 = "167651898641506589161";

        int[] arr1 = new int[s1.length()];
        int[] arr2 = new int[s2.length()];

        for (int i = s1.length() - 1; i > -1; i--) {
            arr1[s1.length() - i - 1] = s1.charAt(i) - '0';
        }
        for (int i = s2.length() - 1; i > -1; i--) {
            arr2[s2.length() - i - 1] = s2.charAt(i) - '0';
        }

        List<Integer> arr = add(arr1, arr2);
        Collections.reverse(arr);

//        System.out.println(Arrays.toString(arr1));
        System.out.println(arr.toString());
    }

    private static List<Integer> add(int[] arr1, int[] arr2) {
        int t = 0;
        List<Integer> arr = new ArrayList<>();
        for (int i = 0; i < arr1.length || i < arr2.length || t != 0; i++) {
            if (i < arr1.length) t += arr1[i];
            if (i < arr2.length) t += arr2[i];
            arr.add(t % 10);
            t /= 10;
        }
        return arr;
    }
}
